package com.controller;

import com.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.Kernel;
import java.io.IOException;
import java.util.Enumeration;

@Api(value = "openMiniProgramApi", tags = {"OpenXiaoChengXu"})
@CrossOrigin
@RestController
@RequestMapping("/miniprogram")
public class OpenProgramController {
    /**
     * 支付宝小程序
     */
    @ApiOperation(value = "ali", httpMethod = "GET", notes = "ali")
    @RequestMapping(value = "/open/ali", method = RequestMethod.GET)
    public Result<String> ali(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new Result<String>().ok("攻城狮在努力开发中,敬请期待....");
    }

    /**
     * 微信
     */
    @ApiOperation(value = "wx", httpMethod = "GET", notes = "wx")
    @RequestMapping(value = "/open/wx", method = RequestMethod.GET)
    public Result<String> wx(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new Result<String>().ok("请使用微信扫一扫打开小程序");
    }


    /**
     * 其他平台
     */
    @ApiOperation(value = "other", httpMethod = "GET", notes = "other")
    @RequestMapping(value = "/open/other", method = RequestMethod.GET)
    public Result<String> other(HttpServletRequest request, HttpServletResponse response) throws Exception {

        return new Result<String>().ok("请用微信或者支付宝扫描二维码");
    }

    @ApiOperation(value = "open", httpMethod = "GET", notes = "open")
    @RequestMapping("/open")
    public void open(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userAgent = request.getHeader("user-agent");
        System.out.println("userAgent : " + userAgent);
        String platform  = "unknow";
        if (userAgent != null && userAgent.contains("AlipayClient")) {
            platform = "ali";
            String url = getPs("https://www.houluzhai.top/miniprogram/open/ali",request,response);
            response.sendRedirect(url);
        } else if (userAgent != null && userAgent.contains("MicroMessenger")) {
            platform = "wx";
            String url = getPs("https://www.houluzhai.top/miniprogram/open/wx",request,response);
            response.sendRedirect(url);
        }else{
            String url = getPs("https://www.houluzhai.top/miniprogram/open/other",request,response);
            response.sendRedirect(url);
        }
    }

    private String getPs(String url,HttpServletRequest request, HttpServletResponse response){
        Enumeration<String> parameterNames = request.getParameterNames();

        String ps = "?";
        while (parameterNames.hasMoreElements()){
            String name = (String)parameterNames.nextElement();//调用nextElement方法获得元素
            String value = request.getParameter(name);
            ps+=name+"="+value+"&";
        }
        if (ps.length()>1){
            ps = ps.substring(0,ps.length()-1);
        }else{
            ps = "";
        }
        url = url+ps;
        System.out.println("url:"+url);
        return url;
    }


}
